package cosplay.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import cosplay.DAO.TagDAOImpl;
import cosplay.beans.Tags;
@Controller
public class TagController extends MultiActionController{
	@Autowired
	private TagDAOImpl tagdao;

	@RequestMapping (value="edittag.htm",method=RequestMethod.GET)
	protected ModelAndView handleshowedit(HttpServletRequest req,
			HttpServletResponse res) throws Exception {
		// TODO Auto-generated method stub
		int id=Integer.valueOf(req.getParameter("id")).intValue();
		Tags tag=tagdao.get(id);
		String message="Please input the new tag info";
		ModelMap model=new ModelMap();
		model.addAttribute("message",message);
		model.addAttribute("tag",tag);
		model.addAttribute("target", "edittag.htm");
		return new ModelAndView("addtag","model",model);
	}
	
	@RequestMapping (value="edittag.htm",method=RequestMethod.POST)
	protected ModelAndView handelactedit(HttpServletRequest req,HttpServletResponse res) throws Exception
	{
		String message="";
		String Tagname=req.getParameter("tagname");
		int id=Integer.valueOf(req.getParameter("id")).intValue();
		boolean sys=true;
		Tags tag=new Tags();
		tag.setId(id);
		tag.setSystem(sys);
		tag.setWord(Tagname);
		tagdao.editSys(tag, id);
		message="Update Sucessful";
		ModelMap model=new ModelMap();
		model.addAttribute("message",message);
		model.addAttribute("tag", tag);
		model.addAttribute("target", "edittag.htm");
		res.sendRedirect("tagadmin.htm");
		return new ModelAndView("addtag","model",model);
	}
	
	@RequestMapping (value="addtag.htm",method=RequestMethod.GET)
	protected ModelAndView handleshowadd(HttpServletRequest arg0,
			HttpServletResponse arg1) throws Exception {
		// TODO Auto-generated method stub
		String message="Please input the tag info";
		ModelMap model=new ModelMap();
		model.addAttribute("message",message);
		model.addAttribute("target", "addtag.htm");
		return new ModelAndView("addtag","model",model);
	}
	
	@RequestMapping (value="addtag.htm",method=RequestMethod.POST)
	protected ModelAndView handelactadd(HttpServletRequest req,HttpServletResponse res) throws Exception
	{
		String message="";
		String Tagname=req.getParameter("tagname");
		boolean sys=true;
		Tags tag=new Tags();
		tag.setSystem(sys);
		tag.setWord(Tagname);
		tagdao.addSys(tag);
		message="Add Sucessful, add another tag";
		ModelMap model=new ModelMap();
		model.addAttribute("message",message);
		model.addAttribute("target", "addtag.htm");
		return new ModelAndView("addtag","model",model);
	}
	
	@RequestMapping(value="tagadmin.htm")
	protected ModelAndView handeladmin(HttpServletRequest req, HttpServletResponse res)
	throws Exception{
		ModelMap model=new ModelMap();
		model.addAttribute("tags",tagdao.getAllForAdmin());
		model.addAttribute("message",req.getParameter("message"));
		return new ModelAndView("taglist","model",model);
	}
	
	@RequestMapping(value="deltag.htm")
	protected ModelAndView handeldel(HttpServletRequest req, HttpServletResponse res)
	throws Exception{
		if (cosplay.Services.FileServices.Stringintcheck(req.getParameter("id")))
		{
			int id=Integer.valueOf(req.getParameter("id")).intValue();
			tagdao.delSys(id);
			res.sendRedirect("tagadmin.htm?message=succed");
		}
		else
		{
			res.sendRedirect("tagadmin.htm?message=failed");
		}
		return null;
	}

}
